package sbu.cs.ui.widget;

import sbu.cs.R;
import sbu.cs.Settings;
import android.content.Context;
import android.graphics.Color;
import android.text.InputFilter;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class UserInputView extends LinearLayout {

	private static final int K_SUBMIT_BUTTON_ID = 200;
	private static final int K_RESET_BUTTON_ID = 201;
	private static final int K_ET_MESSAGE_ID = 202;
	private static final int K_ET_SUBJECT_ID = 203;

	protected static final String LOG_TAG = "UserInputView";
	
	private static final String DEFAULT_TEXT_MESSAGE = "Type Message Here";
	private static final String DEFAULT_TEXT_SUBJECT = "Type Subject Here";
	
	private static final int[] TIMEOUT_VALS={15*60, 30*60, 1*60*60, 3*60*60, 6*60*60};
	private static final int[] RADII={50, 100, 250, 500};
	
	private TextView textViewTimeout;
	private Spinner spinnerTimeout;
	
	private TextView textViewCategory;
	private Spinner spinnerCategory;
	
	private TextView textViewPriority;
	private Spinner spinnerPriority;
	
	private TextView textViewRadius;
	private Spinner spinnerRadius;
	
	private EditText editTextMessage;
	private EditText editTextSubject;
	
	private Button buttonSubmit;
	private Button buttonReset;
	
	private int timeoutVal;
	private String category;
	private int radius;
	private String message;
	private String priority;
	private String subject;

	private int subEntCount;
	private int messageEntCount;
	
	public static final int EVENT_SUBMIT_BUTTON_PRESSED = 101;
	
	public interface UserInputViewEventListener{
		public void onEvent(int event);
	}
	
	private UserInputViewEventListener eventListener;
	
	public Integer getTimeoutVal() {
		return new Integer(timeoutVal);
	}

	public String getCategory() {
		return category;
	}

	public Integer getRadius() {
		return new Integer(radius);
	}

	public String getMessage() {
		return message;
	}

	public String getPriority() {
		return priority;
	}
	
	public String getSubject() {
		return subject;
	}
	
	public void setEventListener(UserInputViewEventListener eventListener){
		this.eventListener = eventListener;
	}
	
	private OnClickListener buttonClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case K_SUBMIT_BUTTON_ID:
				Log.i(LOG_TAG,spinnerCategory.getSelectedItem().toString());
				
				timeoutVal = TIMEOUT_VALS[spinnerTimeout.getSelectedItemPosition()];
				category = spinnerCategory.getSelectedItem().toString();
				radius = RADII[spinnerRadius.getSelectedItemPosition()];
				message = editTextMessage.getText().toString();
				priority = spinnerPriority.getSelectedItem().toString();
				subject = editTextSubject.getText().toString();
				
				if(subject.length() == 0 || message.length() == 0){
					Toast t = Toast.makeText(getContext(),
							"Empty Fields. Please enter valid inputs",
							Toast.LENGTH_SHORT);
					t.show();
//					resetValues();
				}else if(subject.equals(DEFAULT_TEXT_SUBJECT) || message.equals(DEFAULT_TEXT_MESSAGE)){
					Toast t = Toast.makeText(getContext(),
							"Empty Fields. Please enter valid inputs",
							Toast.LENGTH_SHORT);
					t.show();
//					resetValues();
				}else{
					eventListener.onEvent(EVENT_SUBMIT_BUTTON_PRESSED);
				}
				break;
			case K_RESET_BUTTON_ID:
				resetValues();
				break;
			case K_ET_MESSAGE_ID:
				if(editTextMessage.getText().toString().equals(DEFAULT_TEXT_MESSAGE)){
					editTextMessage.setText("");
				}
				break;
			case K_ET_SUBJECT_ID:
				if(editTextSubject.getText().toString().equals(DEFAULT_TEXT_SUBJECT)){
					editTextSubject.setText("");
				}
				break;
			}
		}
	};
	private OnTouchListener touchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			switch(view.getId()){
			case K_ET_MESSAGE_ID:
				if(editTextMessage.getText().toString().equals(DEFAULT_TEXT_MESSAGE)){
					editTextMessage.setText("");
				}
				break;
			case K_ET_SUBJECT_ID:
				if(editTextSubject.getText().toString().equals(DEFAULT_TEXT_SUBJECT)){
					editTextSubject.setText("");
				}
				break;
			}
			return false;
		}
	};;;
	
	private void resetValues(){
		spinnerCategory.setSelection(0);
		spinnerPriority.setSelection(2);
		spinnerRadius.setSelection(0);
		spinnerTimeout.setSelection(1);
		editTextMessage.setText(DEFAULT_TEXT_MESSAGE);
		editTextSubject.setText(DEFAULT_TEXT_SUBJECT);
		subEntCount = 0;
		messageEntCount = 0;
	}
	
	public UserInputView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context){
		setOrientation(LinearLayout.VERTICAL);
		RelativeLayout l1 = new RelativeLayout(context);
		
		RelativeLayout l5_0 = new RelativeLayout(context);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 
        		LayoutParams.WRAP_CONTENT);
		editTextSubject = new EditText(context);
		//editTextSubject.setText(DEFAULT_TEXT_SUBJECT);
		editTextSubject.setLines(1);
		editTextSubject.setMaxLines(1);
		editTextSubject.setId(K_ET_SUBJECT_ID);
		editTextSubject.setHint(DEFAULT_TEXT_SUBJECT);
		//editTextSubject.setOnClickListener(buttonClickListener);
		editTextSubject.setOnTouchListener(touchListener );
		l5_0.addView(editTextSubject,lp);
		lp = new LayoutParams(LayoutParams.MATCH_PARENT, 
        		LayoutParams.WRAP_CONTENT);
		lp.topMargin = 10;
		addView(l5_0,lp);
		
        RelativeLayout l5 = new RelativeLayout(context);
        lp = new LayoutParams(LayoutParams.MATCH_PARENT, 
        		LayoutParams.WRAP_CONTENT);
		editTextMessage = new EditText(context);
		//editTextMessage.setText(DEFAULT_TEXT_MESSAGE);
		editTextMessage.setLines(2);
		editTextMessage.setMaxLines(6);
		editTextMessage.setId(K_ET_MESSAGE_ID);
		//editTextMessage.setOnClickListener(buttonClickListener);
		editTextMessage.setHint(DEFAULT_TEXT_MESSAGE);
		editTextMessage.setOnTouchListener(touchListener );
		l5.addView(editTextMessage,lp);
		addView(l5);
		
		RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		textViewTimeout = new TextView(context);
		textViewTimeout.setText("Timeout");
		textViewTimeout.setGravity(Gravity.CENTER);
		textViewTimeout.setTextSize(18);
		rlp.leftMargin=48;
		rlp.topMargin=8;
		l1.addView(textViewTimeout,rlp);
		
		spinnerTimeout = new Spinner(context);
		RelativeLayout.LayoutParams rlpS = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		rlpS.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		spinnerTimeout.setPromptId(R.string.spinner_timeout_prompt);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                context, R.array.timeoutVals, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTimeout.setAdapter(adapter);
        l1.addView(spinnerTimeout,rlpS);
        
        LinearLayout.LayoutParams toprlp = new LinearLayout.LayoutParams(
        		LinearLayout.LayoutParams.WRAP_CONTENT,
        		LinearLayout.LayoutParams.WRAP_CONTENT);
        toprlp.topMargin = 5;
        addView(l1, toprlp);
        
        RelativeLayout l2 = new RelativeLayout(context);
		textViewCategory = new TextView(context);
		textViewCategory.setText("Category");
		rlp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		textViewCategory.setGravity(Gravity.CENTER);
		textViewCategory.setTextSize(18);
		rlp.leftMargin=48;
		rlp.topMargin=8;
		l2.addView(textViewCategory, rlp);
		
		spinnerCategory = new Spinner(context);
		spinnerCategory.setPromptId(R.string.spinner_category_prompt);
		adapter = ArrayAdapter.createFromResource(
                context, R.array.categories, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCategory.setAdapter(adapter);
        l2.addView(spinnerCategory,rlpS);
        addView(l2);
        
        RelativeLayout l3 = new RelativeLayout(context);
		textViewPriority = new TextView(context);
		textViewPriority.setText("Priority");
		rlp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		textViewPriority.setGravity(Gravity.CENTER);
		textViewPriority.setTextSize(18);
		rlp.leftMargin=48;
		rlp.topMargin=8;

		l3.addView(textViewPriority, rlp);
		
		spinnerPriority = new Spinner(context);
		spinnerPriority.setPromptId(R.string.spinner_priority_prompt);
        adapter = ArrayAdapter.createFromResource(
                context, R.array.priorities, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerPriority.setAdapter(adapter);
        l3.addView(spinnerPriority,rlpS);
        addView(l3);
        
        RelativeLayout l4 = new RelativeLayout(context);
		textViewRadius = new TextView(context);
		textViewRadius.setText("Radius");
		rlp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		textViewRadius.setGravity(Gravity.CENTER);
		textViewRadius.setTextSize(18);
		rlp.leftMargin=48;
		rlp.topMargin=8;

		l4.addView(textViewRadius, rlp);
		
		spinnerRadius = new Spinner(context);
		spinnerRadius.setPromptId(R.string.spinner_radius_prompt);
        adapter = ArrayAdapter.createFromResource(
                context, R.array.radii, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerRadius.setAdapter(adapter);
        l4.addView(spinnerRadius,rlpS);
        addView(l4);
        
        RelativeLayout l6 = new RelativeLayout(context);
		buttonSubmit = new Button(context);
		buttonSubmit.setText("Vircast");
		buttonSubmit.setTextSize(18);
		buttonSubmit.setId(K_SUBMIT_BUTTON_ID);
		buttonSubmit.setOnClickListener(buttonClickListener);
		buttonSubmit.setBackgroundColor(Color.GREEN | Color.GRAY);
		rlp = new RelativeLayout.LayoutParams(
				120,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		rlp.leftMargin = 2;
		l6.addView(buttonSubmit,rlp);
		
		buttonReset = new Button(context);
		buttonReset.setText("Reset");
		buttonReset.setId(K_RESET_BUTTON_ID);
		buttonReset.setOnClickListener(buttonClickListener);
		buttonReset.setTextSize(18);
		buttonReset.setBackgroundColor(Color.BLUE | Color.GRAY);
		rlp = new RelativeLayout.LayoutParams(
				120,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		rlp.addRule(RelativeLayout.RIGHT_OF, K_SUBMIT_BUTTON_ID);
		rlp.leftMargin = 75;
		l6.addView(buttonReset,rlp);
		LayoutParams lp_ = new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		lp_.topMargin = 10;
		addView(l6,lp_);
		
		InputFilter[] FilterArray = new InputFilter[1];
		FilterArray[0] = new InputFilter.LengthFilter(140);
		editTextMessage.setFilters(FilterArray);

		InputFilter[] FilterArray1 = new InputFilter[1];
		FilterArray1[0] = new InputFilter.LengthFilter(30);
		editTextSubject.setFilters(FilterArray1);
		
		editTextMessage.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				//if(event.getKeyCode() == KeyEvent.)
				Log.i(LOG_TAG,"event: " + event.getKeyCode());
				if(actionId == EditorInfo.IME_NULL){
					messageEntCount++;
					if(messageEntCount > 5){
						return true;
					}
				}
				return false;
			}
		});
		
		editTextSubject.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				//if(event.getKeyCode() == KeyEvent.)
				Log.i(LOG_TAG,"event: " + event.getKeyCode());
				if(actionId == EditorInfo.IME_NULL){
					subEntCount++;
					if(subEntCount > 1){
						return true;
					}
				}
				return false;
			}
		});

		resetValues();
	}
	
	@Override
	protected void onVisibilityChanged(View changedView, int visibility) {
		// TODO Auto-generated method stub
		if(changedView.equals(this)){
			if(visibility == View.INVISIBLE){
				resetValues();
			}
		}
		super.onVisibilityChanged(changedView, visibility);
	}
}
